Ext.define('TS.view.dilema.PointGrid' , {
	extend: 'TS.lib.Grid',
	alias : 'widget.dilemapointgrid',
	bringMeChild:function(){
		if(!Util.form('dilemapoint')){
			Ext.create('TS.lib.Window',{
				width:850,
				height:500,
				maximizable:true,
				layout:'accordion',
				items:[
					{xtype:'dilemapointquotegrid',title:'Cytaty'},
					{xtype:'dilemapointmoviegrid',title:'Filmy'},
					{xtype:'dilemapointcounterpointgrid',title:'Kontrargumenty'},
					{xtype:'dilemapointrategrid',title:'Oceny'}
				],
				lbar:[{xtype:'dilemapointform',width:300}]
			});
		} else {
			Util.f.clear('dilemapoint');
		}
		Util.each(['Quote','Movie','Counterpoint','Rate'],function(s){
			Util.store('DilemaPoint'+s).removeAll();
		});
	},
	tbar:Factory.toolbar.search([
		Factory.btn._('Dodaj',function(){
			if(!(id = Util.fldVal('dilema->id')))
				return Factory.flyer.bad('Pytanie nie oztało jeszcze zapisane');
			
			this.up('grid').bringMeChild();
			
			Util.fldVal('dilemapoint->#dilema_id',id);
			var yes = this.up('grid').getStore().$className.indexOf('Yes')>0;
			Util.fldVal('dilema_point->side',yes?'1':'0');
		})],
		function(btn){
			var yes = btn.up('grid').getStore().$className.indexOf('Yes')>0;
			return [{
				name:'dilema_id',value:Util.fldVal('dilema->id')
			},{
				name:'dilema_point_side',value:yes?1:'0'
			}]
		}
	//	[Factory.btn.edit('DilemaPoint')]
	),
	frame:false,
	//plugins:[Factory.plugin.edit()],
	listeners:{
		'itemdblclick':function(){
			
			this.bringMeChild();
			var id = this.getSelectionModel().getSelection()[0].internalId;
			
			Util.s.load('DilemaPointCounterpoint',where(
				['dilema_point_counterpoint.dilema_point_id',id],
				['counterpoint_id',id,'=','OR']
			));
			Util.s.load('DilemaPointQuote',where('dilema_point_id',id));
			Util.s.load('DilemaPointMovie',where('dilema_point_id',id));
			Util.s.load('DilemaPointRate',where('dilema_point_id',id));
			
			return true;
		}
	},
	columns: [
	 	{xtype: 'rownumberer'},{
			header:'ID',
			hidden:true,
			dataIndex:'dilema_point_id'
		},{
			header:'Odpowiedź',
			sortable:true,
			dataIndex:'dilema_point_head',
			renderer:function(v,index,rec){
				var tpl ='<div style="padding:10px;white-space:normal;font-size:12px;color:green;font-weight:bold;">{0}</div><div style="padding:10px;white-space:normal;line-height:10px;font-size:9px;color:gray;">{1}</div>';
				return Ext.String.format(tpl,v,rec.data.dilema_point_body);
			}
		},{
			header:'Odpowiedź',
			sortable:true,
			hidden:true,
			dataIndex:'dilema_point_head'
		},{
			header:'Rozwinięcie',
			hidden:true,
			dataIndex:'dilema_point_body'
		},{
			header:'Autor',
			hidden:true,
			dataIndex:'user_id'
		},{
			header:'Modyfikowano',
			hidden:true,
			dataIndex:'dilema_point_date'
		}
	]
});